﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace PingTool
{
	static class Program
	{
		[DllImport("User32.dll")]
		private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
		[DllImport("User32.dll")]
		private static extern bool SetForegroundWindow(IntPtr hWnd);
		private const int SW_RESTORE = 9;

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main()
		{
			bool bMutexWasCreated;
			Mutex oMutex = new Mutex(true, "PingTool", out bMutexWasCreated);
			if (bMutexWasCreated)
			{
				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);
				Application.Run(new Form1());
				oMutex.ReleaseMutex();
			}
			else
			{
				foreach (Process oProcess in Process.GetProcessesByName("PingTool"))
				{
					if (oProcess.MainWindowHandle != IntPtr.Zero)
					{
						ShowWindow(oProcess.MainWindowHandle, SW_RESTORE);
						SetForegroundWindow(oProcess.MainWindowHandle);
					}
				}
			}
		}
	}
}